<template>
    <div class="admin-page">
        <div class="page-header">
            <div style="display: flex;justify-content: space-between">
                <div style="display: flex; flex-wrap: wrap; align-items: center; gap: 15px; margin-bottom: 15px;">
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">日期:</span>
                        <el-date-picker
                            v-model="searchForm.date"
                            type="date"
                            size="small"
                            style="width: 130px;"
                            placeholder="选择日期"
                            value-format="yyyy-MM-dd"
                        />
                    </div>
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">状态:</span>
                        <el-select v-model="searchForm.status" size="small" style="width: 110px;" placeholder="全部" clearable>
                            <el-option
                                v-for="item in statusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                </div>
                <div style="display: flex; align-items: center; gap: 15px; margin-bottom: 15px;">
                    <el-button type="primary" size="small" @click="handleSearch">搜索</el-button>
                    <el-button type="success" size="small" style="margin-left: 10px;" @click="handleReset">重置</el-button>
                </div>
            </div>

            <el-table :data="tableData" height="560" border style="width: 100%;overflow-y: auto"
                      :header-cell-style="{background:'#FAFAFA',color:'#666', fontSize:'13px'}" size="small">
                <el-table-column prop="id" label="ID" width="80" align="center" />
                <el-table-column prop="date" label="日期" width="120" align="center" />
                <el-table-column prop="amount" label="补贴转余粮" align="center">
                    <template slot-scope="scope">
                        {{ scope.row.amount.toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="状态" width="100" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.status? 'success' : 'info'">
                            {{ scope.row.status? '已转' : '未转' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center" />
            </el-table>

            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total"
            />
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            searchForm: {
                date: '',
                status: ''
            },
            tableData: [],
            statusOptions: [
                { label: '全部', value: '' },
                { label: '已转', value: 1 },
                { label: '未转', value: 0 }
            ],
            currentPage: 1,
            pageSize: 15,
            total: 0
        }
    },
    created() {
        this.getTableData()
    },
    methods: {
        // 模拟请求后端接口获取数据，实际应替换为真实接口调用
        getTableData() {
            const params = {
                page: this.currentPage,
                limit: this.pageSize,
                ...this.searchForm
            };
            // 这里使用 setTimeout 模拟异步请求
            setTimeout(() => {
                const now = new Date();
                const mockData = Array(30).fill(null).map((_, index) => ({
                    id: index + 1,
                    date: `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`,
                    amount: Math.floor(Math.random() * 10000),
                    status: Math.random() > 0.5? 1 : 0,
                    remark: '余粮发放'
                }));
                // 假设后端返回的数据结构
                const response = {
                    data: mockData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize),
                    total: mockData.length
                };
                this.tableData = response.data;
                this.total = response.total;
            }, 1000);
        },
        handleSearch() {
            this.currentPage = 1;
            this.getTableData();
        },
        handleReset() {
            this.searchForm = {
                date: '',
                status: ''
            };
            this.currentPage = 1;
            this.getTableData();
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.currentPage = 1;
            this.getTableData();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getTableData();
        }
    }
}
</script>

<style scoped lang="less">
.admin-page {
    padding: 20px;

    .search-label {
        margin-right: 10px;
        color: #606266;
    }

    .el-table {
        margin-top: 20px;
    }

    .el-pagination {
        margin-top: 20px;
        text-align: right;
    }
}
</style>
